/*
 * SplashScreen.java
 *
 * Created on May 6, 2005, 11:16 AM
 */
package com.bff.startup;

import com.bff.bjj.utils.Resources;
import com.bff.mpd.musicjukebox.lib.JukeboxProperties;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 *
 * @author  Bill
 */
public class StartupSplashScreen extends JFrame implements MouseListener {

    private boolean stopped;
    private StartUpProperties.StartupType type;
    private boolean mouseClicked;

    public StartupSplashScreen(StartUpProperties.StartupType type) {
        this();
        this.type = type;
    }

    public StartupSplashScreen() {
        super();
        setIconImage(Resources.getIcon(Resources.ICON_KEY.TRAY).getImage());
        init();
    }

    private void init() {
        initComponents();
        addMouseListener(this);

        taCenter.addMouseListener(this);

        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle window = getBounds();
        setLocation((screen.width - window.width) / 2, (screen.height - window.height) / 2);

        final Runnable closerRunner = new Runnable() {

            @Override
            public void run() {
                dispose();
            }
        };

        Runnable waitRunner = new Runnable() {

            @Override
            public void run() {
                try {
                    while (!isStopped()) {
                        Thread.sleep(100);
                    }
                    SwingUtilities.invokeAndWait(closerRunner);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(StartupSplashScreen.class.getName()).log(Level.SEVERE, null, ex);
                }
                setStopped(true);

                if (!isMouseClicked()) {
                    switch (getType()) {
                        case MPD:
                            StartApp.runMPD();
                            break;

                        case SQUEEZE:
                            StartApp.runSqueezeCenter();
                            break;
                    }
                }
            }
        }).start();

        setVisible(true);
        Thread splashThread = new Thread(waitRunner);
        splashThread.start();
    }

    public boolean isStopped() {
        return stopped;
    }

    public void setStopped(boolean stopped) {
        setVisible(false);
        this.stopped = stopped;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        titlePanel = new com.bff.bjj.ui.component.BJJPanel();
        lblTitle = new javax.swing.JLabel();
        detailsPanel = new com.bff.bjj.ui.component.BJJPanel();
        bottomPanel = new com.bff.bjj.ui.component.BJJPanel();
        loadProgressBar = new javax.swing.JProgressBar();
        lblProgressbarLabel = new javax.swing.JLabel();
        jPanel2 = new com.bff.bjj.ui.component.BJJPanel();
        taCenter = new javax.swing.JTextArea();
        jPanel1 = new com.bff.bjj.ui.component.BJJPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setTitle(Resources.getMessage(Resources.LABEL_KEY.TITLE));

        titlePanel.setBackground(new java.awt.Color(255, 255, 255));
        titlePanel.setBorder(javax.swing.BorderFactory.createCompoundBorder());
        titlePanel.setPreferredSize(new java.awt.Dimension(350, 45));

        lblTitle.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 24));
        lblTitle.setForeground(new java.awt.Color(0, 51, 255));
        lblTitle.setText(Resources.getMessage(Resources.LABEL_KEY.TITLE));
        lblTitle.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        titlePanel.add(lblTitle);

        getContentPane().add(titlePanel, java.awt.BorderLayout.NORTH);

        detailsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(Resources.getMessage(Resources.LABEL_KEY.STARTUP_DETAILS)));
        detailsPanel.setLayout(new java.awt.BorderLayout());

        bottomPanel.setBackground(new java.awt.Color(0, 0, 0));
        bottomPanel.setForeground(new java.awt.Color(255, 255, 255));
        bottomPanel.setLayout(new java.awt.BorderLayout());

        loadProgressBar.setIndeterminate(true);
        loadProgressBar.setString(Resources.getMessage(Resources.LABEL_KEY.STARTUP_STARTING));
        loadProgressBar.setStringPainted(true);
        bottomPanel.add(loadProgressBar, java.awt.BorderLayout.SOUTH);

        lblProgressbarLabel.setBackground(new java.awt.Color(51, 51, 51));
        lblProgressbarLabel.setForeground(new java.awt.Color(255, 255, 255));
        lblProgressbarLabel.setText(Resources.getMessage(Resources.LABEL_KEY.STARTUP_LOADING));
        bottomPanel.add(lblProgressbarLabel, java.awt.BorderLayout.WEST);

        detailsPanel.add(bottomPanel, java.awt.BorderLayout.SOUTH);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setLayout(new java.awt.BorderLayout());

        taCenter.setEditable(false);
        taCenter.setText(Resources.getMessage(Resources.LABEL_KEY.ABOUT_TEXT, new Object[]{JukeboxProperties.getInstance().getVersion()}));
        taCenter.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        jPanel2.add(taCenter, java.awt.BorderLayout.CENTER);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jLabel1.setBackground(new java.awt.Color(153, 153, 153));
        jLabel1.setIcon(Resources.getIcon(Resources.ICON_KEY.HEADBANG));
        jPanel1.add(jLabel1, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel1, java.awt.BorderLayout.WEST);

        jLabel2.setBackground(new java.awt.Color(255, 255, 255));
        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText(Resources.getMessage(Resources.LABEL_KEY.STARTUP_SELECTION));
        jLabel2.setOpaque(true);
        jPanel2.add(jLabel2, java.awt.BorderLayout.SOUTH);

        detailsPanel.add(jPanel2, java.awt.BorderLayout.CENTER);

        getContentPane().add(detailsPanel, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bottomPanel;
    private javax.swing.JPanel detailsPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblProgressbarLabel;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JProgressBar loadProgressBar;
    private javax.swing.JTextArea taCenter;
    private javax.swing.JPanel titlePanel;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the type
     */
    public StartUpProperties.StartupType  getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(StartUpProperties.StartupType  type) {
        this.type = type;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
        setMouseClicked(true);
        setVisible(false);
        setStopped(true);

        StartupFrame frame = new StartupFrame();
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle window = frame.getBounds();
        frame.setLocation((screen.width - window.width) / 2, (screen.height - window.height) / 2);
        frame.setVisible(true);
        dispose();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    /**
     * @return the mouseClicked
     */
    public boolean isMouseClicked() {
        return mouseClicked;
    }

    /**
     * @param mouseClicked the mouseClicked to set
     */
    public void setMouseClicked(boolean mouseClicked) {
        this.mouseClicked = mouseClicked;
    }
}
